package com.scoremg.api;

import com.scoremg.entity.contants.Constants;
import com.scoremg.entity.dto.WebSessionUserDto;
import com.scoremg.entity.vo.LayuiResultVO;
import com.scoremg.entity.vo.ResponseVO;

import com.scoremg.entity.enums.ResponseCodeEnum;

import javax.servlet.http.HttpSession;


public class ABaseController {

    protected static final String STATUS_SUCCESS = "success";
    protected static final String STATUS_ERROR = "error";

    protected <T> ResponseVO getSuccessResponseVO(T t) {
        ResponseVO<T> responseVO = new ResponseVO<T>();
        responseVO.setStatus(STATUS_SUCCESS);
        responseVO.setCode(ResponseCodeEnum.CODE_200.getCode());
        responseVO.setInfo(ResponseCodeEnum.CODE_200.getMsg());
        responseVO.setData(t);
        return responseVO;
    }

    protected <T> LayuiResultVO getSuccessLayuiResultVO(T t) {
        LayuiResultVO<T> responseVO = new LayuiResultVO<T>();
        responseVO.setStatus(STATUS_SUCCESS);
        responseVO.setCode(ResponseCodeEnum.LAYUI_CODE_0.getCode());
        responseVO.setInfo(ResponseCodeEnum.LAYUI_CODE_0.getMsg());
        responseVO.setData(t);
        return responseVO;
    }

    /**
     * 从session 中获取用户信息
     * @param session
     * @return
     */
    protected WebSessionUserDto getUserInfoFromSession(HttpSession session) {
        WebSessionUserDto webSessionUserDto = (WebSessionUserDto) session.getAttribute(Constants.SESSION_KEY);
        return webSessionUserDto;
    }

}
